﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Tour_TourDepartureAdd : System.Web.UI.UserControl
{
    protected string sTodate = "";
    protected string sFromdate = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            SetLabelLanguage();
            ValidateControl();
            BindDataToLanguageDropDown();
            BindDataToTourDropDown();
            BindDataWhenEdit();
        }
    }

    #region SetLable and validate
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        lblTour.Text = MultipleLanguage.LBL_TOURD[nDefaultLanguage - 1];
        lblTourDepartureDate.Text = MultipleLanguage.LBL_STARTDATE[nDefaultLanguage - 1];
        lblTourReturnDate.Text = MultipleLanguage.LBL_ENDDATE[nDefaultLanguage - 1];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        btnBack.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage - 1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage - 1];
    }
    private void ValidateControl()
    {
        //rfvTourDeparture.ErrorMessage = MessagesValidate.TourDeparture_EMPTY;
        //rfvTourReturnDate.ErrorMessage = MessagesValidate.TourReturnDate_EMPTY;
    }
    #endregion

    #region Relative Functions
    private void BindDataWhenEdit()
    {
        string sAction = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        try
        {
            if (sAction == Config.ACTION_EDIT)
            {
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nID > 0)
                {
                    TourDeparture objTourDeparture = new TourDeparture();
                    objTourDeparture.LoadById(nID);
                    ddlTour.SelectedValue = ConvertData.ConvertToString(objTourDeparture.Data.TourID);

                    if (objTourDeparture.Data.TourDepartureDate > 0)
                        sTodate = ConvertData.ConvertIntToDatetimeString(objTourDeparture.Data.TourDepartureDate);
                    else
                        sTodate = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertDatetimeToInt(DateTime.Today));
                    if (objTourDeparture.Data.TourReturnDate > 0)
                        sFromdate = ConvertData.ConvertIntToDatetimeString(objTourDeparture.Data.TourReturnDate);
                    else
                        sFromdate = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertDatetimeToInt(DateTime.Today));
                }
            }
            else
            {
                sTodate = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertDatetimeToInt(DateTime.Today));
                sFromdate = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertDatetimeToInt(DateTime.Today));
            }
        }
        catch
        {
            string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURDEPARTURE;
            Response.Redirect(sLink);
        }
    }
    private eTourDeparture SetData()
    {
        TourDeparture objTourDeparture = new TourDeparture();
        objTourDeparture.Data.TourID = ConvertData.ConvertToInt(ddlTour.SelectedValue);
        
        string sDate1 = Request.Form["date1"].ToString();
        objTourDeparture.Data.TourDepartureDate = ConvertData.ConvertDateStringToInt(sDate1);
        string sDate2 = Request.Form["date2"].ToString();
        objTourDeparture.Data.TourReturnDate = ConvertData.ConvertDateStringToInt(sDate2);

        return objTourDeparture.Data;
    }
    #endregion

    #region Click Events
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        TourDeparture objTourDeparture = new TourDeparture();

        objTourDeparture.Data = this.SetData();
        int nResult = 0;
        string sLink = "";
        string sAction = Request.QueryString[Config.ACTION];
        switch (sAction)
        {
            case Config.ACTION_ADD:
                nResult = objTourDeparture.Insert();
                if (nResult > 0)
                {
                    sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURDEPARTURE + "&" + Config.MESS_ID + "=1";
                    Response.Redirect(sLink);
                }
                else Alert1.ShowMessage(2, MessagesAlert.INSERT_UNSUCCESSFUL[nDefaultLanguage]);
                break;
            case Config.ACTION_EDIT:
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                nResult = objTourDeparture.Update(nID);
                if (nResult > 0)
                {
                    sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURDEPARTURE + "&" + Config.MESS_ID + "=2";
                    Response.Redirect(sLink);
                }
                else
                    Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage]);
                break;
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURDEPARTURE);
    }
    #endregion

    #region Bind Data Dropdown
    private void BindDataToTourDropDown()
    {
        DataTable dtbTour = new DataTable();
        Tours objTour = new Tours();
        dtbTour = objTour.GetAll();
        ddlTour.DataSource = dtbTour;
        ddlTour.DataValueField = "TourID";
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        switch (nCurrentLanguage)
        {
            case 1:
                ddlTour.DataTextField = "Title1";
                break;
            case 2:
                ddlTour.DataTextField = "Title2";
                break;
            case 3:
                ddlTour.DataTextField = "Title3";
                break;
        }
        ddlTour.DataBind();
    }
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindDataToTourDropDown();
        this.BindDataWhenEdit();
    }
    #endregion
}